#region "Author section"

//<remarks>
//====================================================================
// Name: Feature Event Reciever
// Pupose: To Create Site variation,once feature is get Activated
// Construction Date: 10-Aug-2010
// Author:Kshitij Bishnoi
//====================================================================
//</remarks>

# endregion

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace SiteVariation.Features.SSSiteVariation
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("ebc90b7d-0933-4f2b-8360-de6d12489e23")]
    public class SSSiteVariationEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Overridable Method which is used to handle activated event of feature once activated at site scope.
        /// </summary>
        /// <param name="properties">Give the collection Properties defined in the feature.xml file</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {           
            SPWeb web = null;
            if (properties == null)
            {
                return;
            }

            //getting the current web object and setting it to root web.
            SPWebApplication parentWeb = properties.Feature.Parent as SPWebApplication;
            web = (parentWeb.Sites[0]).RootWeb;

            SPFeaturePropertyCollection featureProperties = properties.Feature.Properties;
            SPFeatureProperty prop = featureProperties[VariationConstants.FeatureProperties.CREATE_VARIATIONS];


            using (SPSite site = new SPSite(web.Site.ID))
            {
                using (SPWeb myWeb = site.OpenWeb(web.ID))
                {
                    try
                    {
                        if (!web.Properties.ContainsKey("VariationLabelsCreated"))
                        {
                            web.Properties.Add("VariationLabelsCreated", "YES");
                            //calling Create Variation to implement site hierarchy, variation labels and variation settings
                            VariationDefinition.CreateVariation(myWeb, featureProperties, parentWeb);
                            web.Properties.Update();//Updating Property Bags.
                            web.Update();//Updating Current web Object.
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }           
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
